package com.qf.service.impl;

import com.qf.mapper.StudentMapper;

import com.qf.pojo.Role;
import com.qf.pojo.Student;
import com.qf.pojo.User;
import com.qf.service.StudentService;
import com.qf.utils.ResultMsg;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    StudentMapper studentMapper;

    @Override
    public ResultMsg selectAllStudent(String name, Integer studentId, Integer studentStatus, Integer page, Integer limit) {
        page = page <= 1 ? 1 : page;
        name = name.equals("") ? null : name;
        studentId = studentId == -1 ? null : studentId;
        studentStatus = studentStatus == -1 ? null : studentStatus;
        List<Student> students = studentMapper.selectAllStudent(name, studentId, studentStatus, (page - 1) * limit, limit);
        if (students.size() == 0) return ResultMsg.FAILED(201, "no data was found");
        return ResultMsg.SUCCESS(0, students, studentMapper.selectStudentCount().getStudentId());
    }

    @Override
    public ResultMsg selectOneStudent(Integer studentId) {
        if (studentId == null) return ResultMsg.FAILED(202, "roleId can not be null");
        List<Student> students = studentMapper.selectStudent(studentId);
        if (students.size() != 1) return ResultMsg.FAILED(201, "no data was found");
        return ResultMsg.SUCCESS(students.get(0));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMsg updateStudent(Student student) {
        if (studentMapper.updateStudent(student) == 0)
            return ResultMsg.FAILED(201, "修改失败");
        return ResultMsg.SUCCESS();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMsg insertStudent(Student student) {
        if(student.getStudentName()==null)
            return ResultMsg.FAILED(202, "参数不能为空");
        if (studentMapper.insertStudent(student) == 0)
            return ResultMsg.FAILED(201, "新增失败");
        return ResultMsg.SUCCESS(0, student, 0);
    }

    @Override
    public ResultMsg updateStudentStatus(List<Integer> studentIds, Integer studentStatus) {
        if (studentStatus != 0 && studentStatus != 1)
            return ResultMsg.FAILED(206, "The userStatus parameter does not allow this value");
        int i = studentMapper.updateStudentStatus(studentIds, studentStatus);
        return new ResultMsg(0, i + " rows of data are successfully modified", null, 0);
    }

    @Override
    public ResultMsg deleteStudent(List<Integer> studentIds) {
        int i = studentMapper.deleteStudent(studentIds);
        if (i > 1)
            return new ResultMsg(0, i + " rows of data are deleted successfully", null, 0);
        else if (i==0)
            return new ResultMsg(201, i + " rows of data are deleted failed", null, 0);
        else
            return new ResultMsg(0, i + " row of data is deleted successfully", null, 0);
    }
}


